package cn.marinda.resturants.controller;

import cn.marinda.resturants.entity.Meal;
import cn.marinda.resturants.global.Pages;
import cn.marinda.resturants.service.MealService;
import cn.marinda.resturants.utils.ApiResult;
import cn.marinda.resturants.utils.PagesResult;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/** 餐品控制器
 * @date 2021/11/24
 * @author Marinda
 *
 */
@RestController
@RequestMapping("/meal")
public class MealController {

    @Resource
    private MealService mealService;

    @RequestMapping("/queryList")
    public ApiResult queryMeal()
    {

        List<Meal> mealList = mealService.queryList();
        return new ApiResult(ApiResult.ON_SUCCESS,"成功",mealList);
    }

    @RequestMapping("/pages")
    public PagesResult selectPages(@RequestParam("page") int page ,@RequestParam("limit") int limit){
        IPage<Meal> mealIpage = mealService.selectPages(new Pages<>(page,limit));
        PagesResult pagesResult = new PagesResult(0,mealIpage.getRecords(),(int)mealIpage.getTotal(),mealIpage.getSize());
        return pagesResult;
    }

    @RequestMapping("/add")
    public ApiResult addMeal(Meal meal){
        return new ApiResult(ApiResult.ON_SUCCESS,"成功",mealService.addMeal(meal));
    }

    @RequestMapping("/delete")
    public ApiResult deleteMeal(String uid){
        if(!mealService.deleteByUid(uid)){
            return new ApiResult(ApiResult.ON_ERROR,"失败",null);
        }
        return new ApiResult(ApiResult.ON_SUCCESS,"成功",null);
    }

    @RequestMapping("/index")
    public ApiResult selectIndexMeal(@RequestParam("mealUid") String mealUid){
        Meal meals = mealService.selectById(mealUid);
        return new ApiResult(ApiResult.ON_SUCCESS,"成功", Arrays.asList(meals));
    }


    @RequestMapping("/like")
    public ApiResult selectLikeNameMeal(String name){
        List<Meal> mealLikeList = mealService.selectLikeByName(name);
        return new ApiResult(ApiResult.ON_SUCCESS,"成功",mealLikeList);
    }

    @RequestMapping("/type")
    public ApiResult selectTypeQueryList(String type){
        List<Meal> mealLikeList = mealService.selectTypeQueryList(type);
        return new ApiResult(ApiResult.ON_SUCCESS,"成功",mealLikeList);
    }

    @RequestMapping("/price")
    public ApiResult selectPriceJoint(BigDecimal startPrice, BigDecimal endPrice){
        List<Meal> mealPriceList = mealService.selectPriceJoint(startPrice, endPrice);

        return new ApiResult(ApiResult.ON_SUCCESS,"成功",mealPriceList);
    }


    @RequestMapping("/number")
    public ApiResult selectDayNumber(String uid){
        int dayNumber = mealService.selectDayNumber(uid);
        return new ApiResult(ApiResult.ON_SUCCESS,"成功",dayNumber);
    }


    @RequestMapping("/update")
    public ApiResult updateMeal(Meal meal){
        return mealService.updateByUid(meal) ? new ApiResult(ApiResult.ON_SUCCESS,"成功",null) : new ApiResult(ApiResult.ON_FAIL,"失败",null);
    }


}
